//
//  ByViewOrderDetailEvaluate.m
//  itboye_shop_ios
//
//  Created by mc on 17/2/13.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewOrderDetailEvaluate.h"
@interface ByViewOrderDetailEvaluate()
{
    NSMutableArray  *_starsArray;
    NSArray         *_evaluateAry;
    UILabel         *_evaluateLabel;
}
@property(nonatomic,assign) NSInteger rate;



@end

@implementation ByViewOrderDetailEvaluate

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupCustomInfo];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setupCustomInfo];
    }
    return self;
}


- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    if (newSuperview) {
        [self setupSubViews];
        if(!_touchEnable){
            self.userInteractionEnabled = NO;
            [self showRatingStar:self.starToShow];
        }
    }
    
}


- (void)setupCustomInfo
{
    _starsArray = [NSMutableArray array];
    _starCount = 5;
    _starSize = CGSizeMake(20, 20);
    _padding = 6;
    _touchEnable = YES;
    _defaultScore = 5;
    _rate = _defaultScore;
    _evaluateAry = @[@"非常差",@"差",@"一般",@"好",@"非常好"];
}

- (void)setupSubViews
{
    for (NSInteger i = 1; i <= self.starCount; i++) {
        UIButton *star = [[UIButton alloc] initWithFrame:CGRectMake(self.padding * i  + (i - 1) * self.starSize.width , 0, self.starSize.width, self.starSize.height)];
        [star setBackgroundImage:[UIImage imageNamed:@"Order_flower_gray"] forState:UIControlStateNormal];
        [star setBackgroundImage:[UIImage imageNamed:@"Order_flower"] forState:UIControlStateSelected];
        [_starsArray addObject:star];
        star.tag = i;
        [star addTarget:self action:@selector(pickupStar:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:star];
        if (i <= self.defaultScore) {
            star.selected = YES;
        }
    }
    CGFloat x = self.padding*5 + 5 *self.starSize.width + 20;
    _evaluateLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, SCREEN_WIDTH - x, self.starSize.height)];
    _evaluateLabel.textColor = [UIColor lightGrayColor];
    _evaluateLabel.font = FONT(14);
    _evaluateLabel.text = @"非常好";
    [self addSubview:_evaluateLabel];
}


- (void)pickupStar:(id)sender
{
    UIButton *star = (UIButton *)sender;
    NSInteger selectedTag = star.tag;
    self.rate = selectedTag;
    if (self.tap) {
        self.tap(self.rate);
    }
    for (NSInteger i = 0; i < selectedTag; i++) {
        UIButton *selectedStar = _starsArray[i];
        selectedStar.selected = YES;
        _evaluateLabel.text = _evaluateAry[i];
    }
    
    for (NSInteger i = selectedTag; i < self.starCount; i++) {
        UIButton *selectedStar = _starsArray[i];
        selectedStar.selected = NO;
    }
}

- (void) showRatingStar : (CGFloat)score
{
    
    NSInteger base = floorf(score);
    for (NSInteger i = 0; i < self.starCount && i < _starsArray.count; i++) {
        UIButton *star = _starsArray[i];
        if (i < base) {
            [star setBackgroundImage:[UIImage imageNamed:@"Order_flower"] forState:UIControlStateNormal];
        } else if (i == base) {
            
            [star setBackgroundImage:[UIImage imageNamed:@"Order_flower_gray"] forState:UIControlStateNormal];
            
            
        } else {
            [star setBackgroundImage:[UIImage imageNamed:@"Order_flower_gray"] forState:UIControlStateNormal];
        }
        _evaluateLabel.text = _evaluateAry[i];
    }
}

- (void)setStarToShow:(CGFloat)starToShow
{
    _starToShow = starToShow;
    [self showRatingStar:starToShow];
}


- (void)setDefaultScore:(CGFloat)defaultScore
{
    _defaultScore = defaultScore;
    _rate = _defaultScore;
}

- (void)resetScoreForCellReuse : (NSInteger)score
{
    UIButton *btn = [UIButton new];
    btn.tag = score;
    [self pickupStar:btn];
}

- (void)prepareForIOS7Show
{
    for (UIView *subView in self.subviews) {
        [subView removeFromSuperview];
    }
    [_starsArray removeAllObjects];
    [self setupSubViews];
    if(!_touchEnable){
        self.userInteractionEnabled = NO;
        [self showRatingStar:self.starToShow];
    }
    
}


@end
